import Cookie from '../../static/js/cookie'
import StringUtils from '../utils/StringUtils'
import store from '../store/index'

let Base64 = require('js-base64').Base64;

//----cookie name --------/
//打开alert
let alert_open = 'hSlds320KFalert88adssd19open_nw'
//关闭第三方认证
let third_auth_close = 'hSlds320KFthird88auth2019close_nw'
//VConsole控件
let v_console = 'cF6v3Hd7jd28ue2cvuek9console12019_nw'
//微信openId cookie (安全考虑，你懂的)
let WechatOpenId = "cF6HQCd643H55e23FddHihid_nw";
/*微信调试模式*/
let Wxdebug = 'cF6HQCd643H55e23FddHWxdebug'

export default {
    /**
     * 是否打开第三方认证
     * @param open
     */
    isOpenThirdAuth() {
        return Cookie.getCookie(third_auth_close) != 'true';
    },
    /**
     * 打开第三方认证
     * @param open
     */
    openThirdAuth(open) {
        if (open) {
            Cookie.setCookie(third_auth_close, null, 1);
        } else {
            Cookie.setCookie(third_auth_close, "true", 1);
        }
    },
    /**
     * 是否打开alert
     * @param open
     */
    isOpenAlert() {
        return Cookie.getCookie(alert_open) == 'alert';
    },
    /**
     * 打开alert
     * @param open
     */
    openAlert(open) {
        if (open) {
            Cookie.setCookie(alert_open, "alert", 1);
        } else {
            Cookie.setCookie(alert_open, null, 1);
        }
    },
    doAlert(msg) {
        if (this.isOpenAlert()) {
            alert(msg);
        }
    },
    doConsole(msg) {
        if (this.isOpenVConsole()) {
            console.log(msg)
        }
    },
    /*打开微信调试模式*/
    isOpenWxDebug(msg){
        if(typeof msg == 'boolean' || typeof msg == 'Boolean'){
            if(msg){
                Cookie.setCookie(Wxdebug, "Wxdebug", 1);
            }else{
                Cookie.setCookie(Wxdebug, null, 1);
            }
        }
        return Cookie.getCookie(Wxdebug) == 'Wxdebug';
    },
    /**
     * 是否打开VConsole
     */
    isOpenVConsole() {
        return Cookie.getCookie(v_console) == 'debug';
    },
    /**
     * 打开VConsole
     * @param open
     */
    openVConsole(open) {
        if (open) {
            Cookie.setCookie(v_console, "debug", 1);
        } else {
            Cookie.setCookie(v_console, null, 1);
        }
    },
    /**
     * 是否存在支付对应OpenId
     *
     * @return
     */
    existPayOpenId() {
        return StringUtils.isNotBlank(this.getPayOpenId());
    },
    /**
     * 获得支付对应OpenId
     *
     * @return
     */
    getPayOpenId() {
        let payOpenId = localStorage.getItem(WechatOpenId)
        if (StringUtils.isBlank(payOpenId)) {
            if (store.state.isThirdLogin) {
                // TODO 20190806 后期改为 通过thirdId 获取open 或 直接使用thirdId向后端进行支付
                this.updateOauthOpenId(0, third.openId)
                return third.openId
            }
        }
        return payOpenId
    },
    /**
     * 更新支付OpenId
     *
     * @param openId
     */
    updatePayOpenId(openId) {
        localStorage.setItem(WechatOpenId, openId)
    }
}
